//
//  YHIMUIKitSystemFaceView.m
//  Meow
//
//  Created by apple on 2019/2/23.
//  Copyright © 2019 yinhe. All rights reserved.
//

#import "YHIMUIKitSystemFaceView.h"

#import "YHIMUIKitSysFaceCell.h"

@interface YHIMUIKitSystemFaceView() <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIPageControl *pageControl;
@end


@implementation YHIMUIKitSystemFaceView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
        [self setupUI];
        [self bindViewModel];
        [self other];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self updateViewsLayout];
}

- (void)initData{
    
}

- (void)setupUI{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.alwaysBounceHorizontal = YES;
    
    
    [self.collectionView registerClass:[YHIMUIKitSystemFacePageCell class] forCellWithReuseIdentifier:NSStringFromClass([YHIMUIKitSystemFacePageCell class])];
    
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor colorWithRed:188 / 255.0 green:188 / 255.0 blue:188 / 255.0 alpha:0.6];
    
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    self.pageControl.pageIndicatorTintColor = [UIColor colorWithRed:188 / 255.0 green:188 / 255.0 blue:188 / 255.0 alpha:1];
    
    [self addSubview:self.lineView];
    [self addSubview:self.collectionView];
    [self addSubview:self.pageControl];
}

- (void)bindViewModel{
    
}

- (void)other{
    
}

- (void)updateViewsLayout{
    self.lineView.frame = CGRectMake(0, 0, self.bounds.size.width, 0.5);
    self.pageControl.frame = CGRectMake(0, self.bounds.size.height - kYHIMUIKIT_SystemFace_PageControll_Height, self.bounds.size.width, kYHIMUIKIT_SystemFace_PageControll_Height);
    self.collectionView.frame = CGRectMake(0, self.lineView.frame.origin.y + self.lineView.frame.size.height + kYHIMUIKIT_SystemFace_Collection_TopMargin, self.bounds.size.width, self.bounds.size.height - self.lineView.frame.origin.y - self.lineView.frame.size.height - kYHIMUIKIT_SystemFace_Collection_TopMargin - kYHIMUIKIT_SystemFace_Collection_BottomMargin - kYHIMUIKIT_SystemFace_PageControll_Height);
}

#pragma mark - Setter
- (void)setSysFaces:(NSArray<NSArray *> *)sysFaces{
    _sysFaces = sysFaces;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
        self.pageControl.numberOfPages = self.sysFaces.count;
    });
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.sysFaces.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YHIMUIKitSystemFacePageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YHIMUIKitSystemFacePageCell class]) forIndexPath:indexPath];
    cell.faces = self.sysFaces[indexPath.section];
    __weak typeof(self) weakSelf = self;
    cell.deleteFaceBlock = ^{
        if (self->_delegate && [self->_delegate respondsToSelector:@selector(sysFaceViewDidDelete:)]) {
            [self->_delegate sysFaceViewDidDelete:weakSelf];
        }
    };
    cell.didSelectFaceBlock = ^(NSString * _Nonnull face) {
        if (self->_delegate && [self->_delegate respondsToSelector:@selector(sysFaceView:didSelectSysFace:)]) {
            [self->_delegate sysFaceView:weakSelf didSelectSysFace:face];
        }
    };
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger page = (NSInteger)roundf(ABS(scrollView.contentOffset.x / scrollView.bounds.size.width));
    self.pageControl.currentPage = page;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return collectionView.bounds.size;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0.0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsZero;
}


@end






@interface YHIMUIKitSystemFacePageCell() <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation YHIMUIKitSystemFacePageCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        self.collectionView.pagingEnabled = NO;
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.backgroundColor = [UIColor clearColor];
        
        
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        [self.collectionView registerClass:[YHIMUIKitSysFaceCell class] forCellWithReuseIdentifier:NSStringFromClass([YHIMUIKitSysFaceCell class])];
        [self.collectionView registerClass:[YHIMUIKitSysDeleteCell class] forCellWithReuseIdentifier:NSStringFromClass([YHIMUIKitSysDeleteCell class])];
        [self.collectionView registerClass:[YHIMUIKitSysEmptyCell class] forCellWithReuseIdentifier:NSStringFromClass([YHIMUIKitSysEmptyCell class])];
        
        [self.contentView addSubview:self.collectionView];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
}

- (void)setFaces:(NSArray *)faces{
    _faces = faces;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
    });
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.faces.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    id face = [self.faces objectAtIndex:indexPath.item];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
    if ([face isKindOfClass:[YHIMUIKitSystemFace class]]) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YHIMUIKitSysFaceCell class]) forIndexPath:indexPath];
        ((YHIMUIKitSysFaceCell *)cell).faceLabel.text = ((YHIMUIKitSystemFace *)face).sysFaceStr;
    } else if ([face isKindOfClass:[YHIMUIKitSystemFaceEmpty class]]) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YHIMUIKitSysEmptyCell class]) forIndexPath:indexPath];
    } else if ([face isKindOfClass:[YHIMUIKitSystemFaceDelete class]]) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YHIMUIKitSysDeleteCell class]) forIndexPath:indexPath];
        ((YHIMUIKitSysDeleteCell *)cell).deleteImageView.image = [UIImage imageNamed:@"message_face_delete"];
    }
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    id face = [self.faces objectAtIndex:indexPath.item];
    if ([face isKindOfClass:[YHIMUIKitSystemFace class]]) {
        if (self.didSelectFaceBlock) {
            self.didSelectFaceBlock(((YHIMUIKitSystemFace *)face).sysFaceStr);
        }
    } else if ([face isKindOfClass:[YHIMUIKitSystemFaceDelete class]]) {
        if (self.deleteFaceBlock) {
            self.deleteFaceBlock();
        }
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat itemWidth = (collectionView.bounds.size.width - kYHIMUIKIT_SystemFace_SectionInset.left - kYHIMUIKIT_SystemFace_SectionInset.right - (kYHIMUIKIT_SystemFace_PerRowCount - 1) * kYHIMUIKIT_SystemFace_InteritemSpace) / kYHIMUIKIT_SystemFace_PerRowCount;
    CGFloat itemHeight = (collectionView.bounds.size.height - kYHIMUIKIT_SystemFace_SectionInset.top - kYHIMUIKIT_SystemFace_SectionInset.bottom - (kYHIMUIKIT_SystemFace_RowCount - 1) * kYHIMUIKIT_SystemFace_LineSpace) / kYHIMUIKIT_SystemFace_RowCount;
    return CGSizeMake(itemWidth, itemHeight);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return kYHIMUIKIT_SystemFace_LineSpace;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return kYHIMUIKIT_SystemFace_InteritemSpace;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return kYHIMUIKIT_SystemFace_SectionInset;
}

@end




@implementation YHIMUIKitSystemFace
@end

@implementation YHIMUIKitSystemFaceDelete
@end

@implementation YHIMUIKitSystemFaceEmpty
@end
